// firmOrder.js
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        couponId: 0,

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        if (options.addressId) {
            wx.request({
                url: app.data.baseUrl + '/AibraUsb/address/updateUI.do',
                data: {
                    addressId: options.addressId
                },
                success: function(res) {
                    that.setData({
                        address: res.data.address.address,
                        region: res.data.address.region,
                        name: res.data.address.name,
                        mobile: res.data.address.mobile
                    })
                }
            })
        }
        wx.request({
            url: app.data.baseUrl + '/AibraUsb/orders/addUI.do',
            data: {
                openId: app.globalData.openid,
                priceId: options.priceId,
                goodsId: options.goodsId,
                count: options.count,

            },
            success: function(res) {
                var ucList = res.data.ucList;
                var ucLists = []
                var ucListL = []
                for (var i = 0; i < ucList.length; i++) {
                    ucLists[i] = '优惠券 ' + ucList[i][1] + ' 元'
                    ucListL[i] = ucList[i][1]
                }
                var totalMoney = Math.floor(res.data.totalMoney * 100) / 100
                that.setData({
                    count: res.data.count,
                    goodsId: res.data.goodsId,
                    points: res.data.points,
                    priceId: res.data.priceId,
                    totalMoney: totalMoney,
                    title: res.data.title,
                    styleName: res.data.styleName,
                    showPicture: res.data.showPicture,
                    ucList: res.data.ucList,
                    ucLists: ucLists,
                    totalPoints: res.data.totalPoints,
                    addressList: res.data.addressList,
                    retailPrice: res.data.retailPrice,
                    ucListL: ucListL,
                    content: options.content,
                    addressId: options.addressId
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
    bindAddress: function() {
        var that = this;
        wx.redirectTo({
            url: 'chooseAddress/chooseAddress?priceId=' + that.data.priceId + '&goodsId=' + that.data.goodsId + '&count=' + that.data.count + '&content=' + that.data.content

        })
    },
    WxPay: function() {
        var that = this;
        if (that.data.addressId == undefined) {
            wx.showToast({
                title: '请先选择地址'
            })
        }

        wx.request({
            url: app.data.baseUrl + '/AibraUsb/orders/doRecharge.do',
            data: {
                count: that.data.count,
                goodsId: that.data.goodsId,
                priceId: that.data.priceId,
                addressId: that.data.addressId,
                userCouponId: that.data.couponId,
                openId: app.globalData.openid
            },
            success: function(res) {
                wx.requestPayment({
                    timeStamp: res.data.timeStamp,
                    nonceStr: res.data.nonceStr,
                    package: res.data.package,
                    signType: res.data.signType,
                    paySign: res.data.paySign,

                    agent: res.data.agent,
                    appId: res.data.appId,
                    packageValue: res.data.packageValue,
                    sendUrl: res.data.sendUrl,
                    success: function(res) {
                        wx.redirectTo({
                            url: '../order/order'
                        })
                    }
                })

            }

        })
    },
    bindPickerChange: function(e) {
        var that = this;
        console.log('picker发送选择改变，携带值为', e.detail.value)
            // that.data.totalMoney = that.data.totalMoney - that.data.ucListL[e.detail.value]

        var totalMoney = that.data.count * that.data.retailPrice;
        if ((totalMoney - 0) - (that.data.ucListL[e.detail.value] - 0) > 0) {
            totalMoney = (totalMoney - 0) - (that.data.ucListL[e.detail.value] - 0)
        }
        totalMoney = totalMoney.toFixed(2)
        this.setData({
            id: e.detail.value,
            totalMoney: totalMoney,
            couponId: that.data.ucList[e.detail.value][0]
        })
    },
})